package com.yrj.shopping;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class ShoppingCart {
    private static final int CART_CAPACITY = 10;
    private static List<Product> products;
    private static int currentCapacity;
    public ShoppingCart() {
        products = new ArrayList<>();
        currentCapacity = 0;
    }
    //添加商品
    public static void addProduct(Product product, int numToAdd) {
        if (currentCapacity + numToAdd > CART_CAPACITY) {
            System.out.println("购物车已满，无法添加更多商品！");
            return;
        }

        if (product.getNum() < numToAdd) {
            System.out.println("商品库存不足，无法添加指定数量！");
            return;
        }

        boolean productExists = false;
        for (Product p : products) {
            if (p.getName().equals(product.getName())) {
                p.setNum(p.getNum() + numToAdd);
                productExists = true;
                break;
            }
        }

        if (!productExists) {
            product.setNum(numToAdd);
            products.add(product);
        }

        currentCapacity += numToAdd;
    }

    //查看购物车中的商品
    public static void viewCart(){
        System.out.println("购物车中的商品有：");
        for (Product p : products) {
            System.out.println(p.getName() + " - " + p.getNum());
        }
    }
    //删除商品
    public static void removeProduct(String productName) {
        for (int i = 0; i < products.size(); i++) {
            if (products.get(i).getName().equals(productName)) {
                currentCapacity -= products.get(i).getNum();
                products.remove(i);
                System.out.println("已删除商品：" + productName);
                return;
            }
        }

        System.out.println("购物车中不存在该商品：" + productName);
    }
    public static void checkout() {
        double totalCost = 0;
        for (Product p : products) {
            totalCost += p.getPrice() * p.getNum();
        }

        System.out.println("结算成功，总金额：" + totalCost);
    }
}

